<?php
// +----------------------------------------------------------------------
// | Fanwe 多语商城建站系统 (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: awfigq(67877605@qq.com)
// +----------------------------------------------------------------------

//积分兑换商品
class ExchangeGoodsAction extends CommonAction{
	//列表
	public function index()
	{
		$name = trim($_REQUEST['name']);
		$status = trim($_REQUEST['status']);
		
		$where="eg.id is not null";
		
		if(!empty($name))
		{
			$this->assign("name",$name);
			$where.=" and (g.name like '%$name%' or s.name like '%$name%' or g.city like '%$name%')";
			$parameter .= "name=" . urlencode($name)."&";
		}
		
		if($status != "" && $status >= 0)
		{
			$where.=" and eg.status = $status";
			$parameter .= "status=" . $status ."&";
			$this->assign("status",$status);
		}
		else
		{
			$this->assign("status",-1);
		}
		
		$sql = 'SELECT eg.*,g.name as goods_name,g.city,s.name as site_name '.
					'FROM '.C("DB_PREFIX").'exchange_goods as eg '.
					'left join '.C("DB_PREFIX").'goods as g on g.id = eg.goods_id '.
					'left join '.C("DB_PREFIX").'site as s on s.id = g.site_id '.
					"where $where";
		
		$model = M();
        $voList = $this->_Sql_list($model, $sql, "&".$parameter, 'id', false);
		
		$this->display ();
		return;
	}
	
	public function add()
	{
		$new_sort = D(MODULE_NAME)->where("status=1") -> max("sort") + 1;
		$this->assign('new_sort',$new_sort);
		
		$this->display ();
	}
	
	public function edit()
	{
		$model = D (MODULE_NAME);
		$id = $_REQUEST [$model->getPk ()];
		$sql = 'SELECT eg.*,g.name as goods_name,g.city,s.name as site_name '.
					'FROM '.C("DB_PREFIX").'exchange_goods as eg '.
					'left join '.C("DB_PREFIX").'goods as g on g.id = eg.goods_id '.
					'left join '.C("DB_PREFIX").'site as s on s.id = g.site_id '.
					"where eg.id = $id";
		
		$eg = M()->query($sql);
		$vo = $eg[0];
		$this->assign ( 'vo', $vo );
		$this->display();
	}
	
	public function setting()
	{
		$arr = D("ExchangeSettings")->findAll();
		$settings = array();
		foreach($arr as $setting)
		{
			$settings[$setting['code']] = $setting['val'];
		}
		
		$this->assign('settings',$settings);
		$this->display ();
	}
	
	public function update_setting()
	{
		foreach($_REQUEST['setting'] as $code => $val)
		{
			$content = $_REQUEST[$code];
			D("ExchangeSettings")->where("code = '$code'")->setField("val",$content);
		}
		
		$this->success (L('EDIT_SUCCESS'));
	}
}

function getInfoUrl($id,$type)
{
	if($type < 2)
		return "<a href='".U("GoodsBond/index",array("goods_id"=>$id))."'>团购券列表</a>";
}
?>